#!/usr/bin/env python

import os, sys, getopt

import MySQLdb

import utility


def save_job_events_to_mysql(cursor, job_events):
    command = "insert into job_events(user_name, job_id) values(%s, %s)"

    inserted_data = []
    for (job_id, user_name) in job_events:
        if len(inserted_data) == 10000:
            cursor.executemany(command, inserted_data)
            inserted_data = []
        inserted_data.append((user_name, job_id))

    if len(inserted_data) != 0:
        cursor.executemany(command, inserted_data)


def save_task_usage_to_mysql(cursor, task_usage):
    command = "insert into task_usage(job_id, task_id, start_time, end_time) values(%s, %s, %s, %s)"

    inserted_data = []
    for (start_time, end_time, job_id, task_id) in task_usage:
        if len(inserted_data) == 10000:
            cursor.executemany(command, inserted_data)
            inserted_data = []
        inserted_data.append((int(job_id), int(task_id), start_time, end_time))

    if len(inserted_data) != 0:
        cursor.executemany(command, inserted_data)


# main framework that trigger the process
def process_data(begin_index, end_index):
    job_events_prefix = "job_events/"
    task_usage_prefix = "task_usage/"
    file_name_prefix = "part-00"
    file_name_suffix = "-of-00500.csv"

    try:
        connection = MySQLdb.connect(
            host="localhost",
            port=3306,
            user="root",
            passwd="123456",
            db="aliyun", 
            )

        cursor = connection.cursor()
        for i in range(begin_index, end_index):
            if i < 10:
                file_name = file_name_prefix+"00"+str(i)+file_name_suffix
            elif i < 100:
                file_name = file_name_prefix+"0"+str(i)+file_name_suffix
            else:
                file_name = file_name_prefix+str(i)+file_name_suffix

            print i
            job_events = utility.extract_job_events(job_events_prefix+file_name)
            save_job_events_to_mysql(cursor, job_events)
            connection.commit()
            
            task_usage = utility.extract_task_usage(task_usage_prefix+file_name)
            save_task_usage_to_mysql(cursor, task_usage)
            connection.commit()
        
        cursor.close()
        connection.close()
    except MySQLdb.Error,e:
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])


def main(argv):
    try:
        opts, args = getopt.getopt(argv[1:], "hb:e:")
    except getopt.GetoptError as err:
        print(err)
        sys.exit(2)


    # set default value
    begin_index = 0
    end_index = 500

    for o, s in opts:
        # help information
        if o == "-h":
            print "usge   -b <begin package index>"
            print "       -e <end package index>"
            return
        elif o == "-b":
            begin_index = int(s)
        elif o == "-e":
            end_index = int(s)

    process_data(begin_index, end_index)
    

if __name__ == "__main__":
    main(sys.argv)